package com.xinyue.game.message;

import com.xinyue.game.utils.ByteBufUtil;
import com.xinyue.game.utils.GameDateUtil;
import io.netty.buffer.ByteBuf;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;

/**
 * @author 王广帅
 * @since 2023/7/20 20:20
 **/
@Data
public class GameServerResponseHeader implements IGameMessageHeader {

    private String requestUri;
    @Setter(AccessLevel.NONE)
    private byte[] requestUriBytes;
    /**
     * 请求唯一标识
     */
    private String requestId;
    @Setter(AccessLevel.NONE)
    private byte[] requestIdBytes;
    /**
     * 用户标识
     */
    private String userKey;

    @Setter(AccessLevel.NONE)
    private byte[] userKeyBytes;
    /**
     * 响应时间
     */
    private long responseTime;

    private int messageType;

    private int errorCode = 0;
    private String errorMsg;
    @Setter(AccessLevel.NONE)
    private byte[] errorMsgBytes;

    @Override
    public int getHeaderTotalSize() {
        this.requestUriBytes = this.requestUri.getBytes(StandardCharsets.UTF_8);
        this.requestIdBytes = requestId.getBytes(StandardCharsets.UTF_8);
        this.userKeyBytes = this.userKey.getBytes(StandardCharsets.UTF_8);
        if (errorCode != 0 && StringUtils.isBlank(errorMsg)) {
            throw new IllegalArgumentException("错误码不为0的情况下，错误信息不能为空");
        }
        int size = 2 + this.requestUri.length() + requestIdBytes.length + 2 + userKeyBytes.length + 8 + 1 + 4;
        if (StringUtils.isNoneBlank(errorMsg)) {
            this.errorMsgBytes = this.errorMsg.getBytes(StandardCharsets.UTF_8);
            size += this.errorMsgBytes.length;
        }
        return size;
    }

    @Override
    public void write(ByteBuf byteBuf) {
        byteBuf.writeShort(this.requestUriBytes.length);
        byteBuf.writeBytes(this.requestUriBytes);
        // 固定长度
        byteBuf.writeBytes(requestIdBytes);

        byteBuf.writeShort(userKeyBytes.length);
        byteBuf.writeBytes(userKeyBytes);

        byteBuf.writeLong(this.responseTime);
        byteBuf.writeByte(this.messageType);
        byteBuf.writeInt(this.errorCode);
        if (this.errorMsgBytes != null) {
            byteBuf.writeBytes(this.errorMsgBytes);
        }
    }

    @Override
    public void read(ByteBuf byteBuf) {
        this.requestUri = ByteBufUtil.readShortString(byteBuf);
        this.requestId = ByteBufUtil.readIntString(32, byteBuf);
        this.userKey = ByteBufUtil.readShortString(byteBuf);
        this.responseTime = byteBuf.readLong();
        this.messageType = byteBuf.readByte();
        this.errorCode = byteBuf.readInt();
        if (this.errorCode != 0) {
            this.errorMsg = ByteBufUtil.readLeftString(byteBuf);
        }
    }

    @Override
    public String toString() {
        StringBuilder str = new StringBuilder();
        str.append("requestUri: ").append(this.requestUri)
                .append(", requestId: ").append(this.requestId)
                .append(", userKey: ").append(this.userKey)
                .append(", responseTime: ").append(GameDateUtil.getStandardDate(this.responseTime))
                .append(", messageType: ").append(this.messageType);
        return str.toString();
    }


}
